package com.biomatch.response;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.biomatch.dto.Pagination;

import java.util.Map;

/**
 * 封装了所有返回结果
 */
public class RestResponse {

    private final static String SUCCESS = "success";

    public static <T> RestResult<T> success() {
        return new RestResult<T>().setCode(RestCode.SUCCESS).setMsg(SUCCESS);
    }

    public static <T> RestResult<T> success(T data) {
        return new RestResult<T>().setCode(RestCode.SUCCESS).setMsg(SUCCESS).setData(data);
    }

    public static <T> RestResult<T> success(IPage<T> data) {
        return new RestResult<T>().setCode(RestCode.SUCCESS).setMsg(SUCCESS).setPagination(data);
    }

    public static <T> RestResult<T> error(String message) {
        return new RestResult<T>().setCode(RestCode.FAIL).setData(null).setMsg(message);
    }

    public static <T> RestResult<T> error(int code, String msg) {
        return new RestResult<T>().setCode(code).setMsg(msg);
    }

    public static <T> RestResult<T> error(int code, String msg, T data) {
        return new RestResult<T>().setCode(code).setMsg(msg).setData(data);
    }

    public static <T> RestResult<T> mapResult(Map map) {
        if(((boolean)map.get("success"))){
            return new RestResult<T>().setCode(RestCode.SUCCESS).setMsg((String)map.get("msg"));
        }
        return new RestResult<T>().setCode(RestCode.FAIL).setMsg((String)map.get("msg"));

    }
}
